<template>
  <div class="login-container">
    <!-- 左侧装饰 -->
    <div class="login-left">
      <div class="decoration-content">
        <h1 class="brand-title">
          <el-icon :size="48"><Reading /></el-icon>
          教案管理平台
        </h1>
        <p class="brand-description">
          让教学更简单，让备课更高效
        </p>
        <div class="feature-list">
          <div class="feature-item">
            <el-icon><CircleCheck /></el-icon>
            <span>智能化教案管理</span>
          </div>
          <div class="feature-item">
            <el-icon><CircleCheck /></el-icon>
            <span>多学科全年级覆盖</span>
          </div>
          <div class="feature-item">
            <el-icon><CircleCheck /></el-icon>
            <span>云端存储随时访问</span>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 右侧登录表单 -->
    <div class="login-right">
      <div class="login-card">
        <div class="login-header">
          <h2 class="login-title">欢迎回来</h2>
          <p class="login-subtitle">请登录您的账户</p>
        </div>
        
        <el-form 
          ref="loginFormRef"
          :model="loginForm" 
          :rules="loginRules"
          @submit.prevent="handleLogin"
          class="login-form"
        >
          <el-form-item prop="email">
            <el-input 
              v-model="loginForm.email" 
              type="email"
              size="large"
              placeholder="邮箱地址"
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <el-icon><Message /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input 
              v-model="loginForm.password" 
              type="password"
              size="large"
              placeholder="密码"
              show-password
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item>
            <div class="login-options">
              <el-checkbox v-model="loginForm.rememberMe">
                <span class="remember-text">记住我</span>
              </el-checkbox>
              <router-link to="/forgot-password" class="forgot-link">
                忘记密码？
              </router-link>
            </div>
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              size="large"
              :loading="loading"
              @click="handleLogin"
              class="login-button"
              round
            >
              <span v-if="!loading">立即登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>
          
          <div class="divider">
            <span class="divider-text">或</span>
          </div>
          
          <el-form-item>
            <el-button 
              size="large"
              class="oauth-button"
              round
              @click="handleOAuthLogin('wechat')"
            >
              <el-icon><ChatDotRound /></el-icon>
              微信登录
            </el-button>
          </el-form-item>
          
          <div class="register-link">
            <span>还没有账户？</span>
            <router-link to="/register">立即注册</router-link>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import {
  Message,
  Lock,
  Reading,
  CircleCheck,
  ChatDotRound
} from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()

// 表单引用
const loginFormRef = ref<FormInstance>()

// 表单数据
const loginForm = reactive({
  email: '',
  password: '',
  rememberMe: false
})

// 表单验证规则
const loginRules = reactive<FormRules>({
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: ['blur', 'change'] }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
})

// 加载状态
const loading = ref(false)

// 页面加载时检查记住的账号
onMounted(() => {
  const rememberedEmail = localStorage.getItem('rememberedEmail')
  if (rememberedEmail) {
    loginForm.email = rememberedEmail
    loginForm.rememberMe = true
  }
})

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (!valid) return
    
    loading.value = true
    try {
      await authStore.login({
        email: loginForm.email,
        password: loginForm.password
      })
      
      // 处理记住我
      if (loginForm.rememberMe) {
        localStorage.setItem('rememberedEmail', loginForm.email)
      } else {
        localStorage.removeItem('rememberedEmail')
      }
      
      ElMessage.success('登录成功！')
      
      // 跳转到之前的页面或教案列表页
      const redirect = route.query.redirect as string || '/lesson-plans'
      await router.push(redirect)
    } catch (error: any) {
      ElMessage.error(error.message || '登录失败，请检查邮箱和密码')
    } finally {
      loading.value = false
    }
  })
}

// 处理第三方登录
const handleOAuthLogin = (provider: string) => {
  ElMessage.info(`${provider}登录功能开发中...`)
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  background: #f0f2f5;
}

/* 左侧装饰区域 */
.login-left {
  flex: 1;
  background: linear-gradient(135deg, #409eff 0%, #67c23a 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.login-left::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 0%, transparent 70%);
  animation: rotate 30s linear infinite;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.decoration-content {
  position: relative;
  z-index: 1;
  color: white;
  text-align: center;
  padding: 0 40px;
}

.brand-title {
  font-size: 36px;
  font-weight: 700;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.brand-description {
  font-size: 18px;
  margin-bottom: 48px;
  opacity: 0.9;
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
  text-align: left;
  max-width: 300px;
  margin: 0 auto;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 16px;
  opacity: 0.9;
}

.feature-item .el-icon {
  font-size: 20px;
}

/* 右侧登录区域 */
.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.login-card {
  background: white;
  padding: 48px;
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.08);
  width: 100%;
  max-width: 480px;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-title {
  margin: 0;
  color: #303133;
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 8px;
}

.login-subtitle {
  margin: 0;
  color: #606266;
  font-size: 16px;
}

.login-form {
  width: 100%;
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.remember-text {
  color: #606266;
  font-size: 14px;
}

.forgot-link {
  color: #409eff;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s;
}

.forgot-link:hover {
  color: #66b1ff;
}

.login-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
}

.divider {
  position: relative;
  text-align: center;
  margin: 24px 0;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #e4e7ed;
}

.divider-text {
  position: relative;
  padding: 0 16px;
  background: white;
  color: #909399;
  font-size: 14px;
}

.oauth-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  border: 1px solid #e4e7ed;
  color: #606266;
  background: #f5f7fa;
  transition: all 0.3s;
}

.oauth-button:hover {
  background: #ecf5ff;
  border-color: #409eff;
  color: #409eff;
}

.oauth-button .el-icon {
  margin-right: 8px;
  font-size: 18px;
}

.register-link {
  text-align: center;
  color: #606266;
  font-size: 14px;
  margin-top: 24px;
}

.register-link a {
  color: #409eff;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s;
}

.register-link a:hover {
  color: #66b1ff;
}

/* 表单样式优化 */
:deep(.el-form-item) {
  margin-bottom: 24px;
}

:deep(.el-input__wrapper) {
  padding: 12px 16px;
  border-radius: 8px;
  transition: all 0.3s;
}

:deep(.el-input__wrapper:hover) {
  border-color: #c0c4cc;
}

:deep(.el-input__wrapper.is-focus) {
  border-color: #409eff;
  box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.1);
}

:deep(.el-input__prefix) {
  font-size: 18px;
  color: #909399;
}

:deep(.el-checkbox) {
  height: auto;
}

:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #409eff;
  border-color: #409eff;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .login-left {
    display: none;
  }
  
  .login-right {
    flex: 1;
    background: linear-gradient(135deg, #409eff 0%, #67c23a 100%);
  }
  
  .login-card {
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
  }
}

@media (max-width: 480px) {
  .login-right {
    padding: 20px;
  }
  
  .login-card {
    padding: 32px 24px;
  }
  
  .login-title {
    font-size: 24px;
  }
  
  .login-subtitle {
    font-size: 14px;
  }
}
</style>